package applet;

import processing.core.PApplet;

public class ArtistButton extends Button {

	public boolean pressed = false;
	public boolean dragged = false;
	private int prevPaintX, prevPaintY;
	private int dx, dy;
	
	public Artist artist;

	@Override
	public void draw()
	{
		int cx = x;
		int cy = y;
		
		

		if(mouseOver()) //draw the menu of sub-categories
		{
			if(!active)
				if(GLOBAL.processing.mousePressed)
				{
					//GLOBAL.processing.fill(GLOBAL.colorButtonActive);
					GLOBAL.gu.drawBox(x, y, width, height, 0, GLOBAL.processing.color(150, 150, 200), 255);
					GLOBAL.gu.drawBox(x, y, width, height, 1, GLOBAL.processing.color(150, 150, 200), 150);
					cx += 1;
					cy += 1;
				}
				else
				{
					GLOBAL.processing.fill(GLOBAL.colorButtonBackground);
					GLOBAL.processing.rectMode(PApplet.CORNER);
					GLOBAL.processing.rect(x, y, width, height);
					GLOBAL.gu.drawBox(x, y, width, height, 0, GLOBAL.processing.color(170, 150, 200), 255);
					GLOBAL.gu.drawBox(x, y, width, height, 1, GLOBAL.processing.color(170, 150, 200), 150);
				}
			else
			{
				if(GLOBAL.processing.mousePressed)
				{
					cx+=1;
					cy+=1;
					GLOBAL.processing.fill(GLOBAL.colorButtonActive,0);
				}
				
				else
				{
					GLOBAL.processing.fill(GLOBAL.colorButtonActive,255);
					GLOBAL.processing.rectMode(PApplet.CORNER);
					GLOBAL.processing.rect(x, y, width, height);
				}
				GLOBAL.gu.drawBox(x, y, width, height, 0, GLOBAL.processing.color(255, 0, 0), 255);
				GLOBAL.gu.drawBox(x, y, width, height, 1, GLOBAL.processing.color(255, 0, 0), 150);
			}
				
		}
		else
		{
			if(active)
			{
				GLOBAL.processing.noStroke();
				GLOBAL.processing.fill(GLOBAL.colorButtonActive);
				GLOBAL.processing.rect(x, y, width, height);
				GLOBAL.gu.drawBox(x, y, width, height, 0, GLOBAL.processing.color(150, 150, 200), 255); //frame of the button
				GLOBAL.gu.drawBox(x, y, width, height, 1, GLOBAL.processing.color(150, 150, 200), 150);
			}
			else
			{
				GLOBAL.processing.rectMode(PApplet.CORNER);

		    	GLOBAL.processing.noStroke();
		    	GLOBAL.processing.fill(GLOBAL.colorButtonBackground/*backColor*/);
		    	GLOBAL.processing.rect(x, y, width, height);
		    	
		    	GLOBAL.gu.drawBox(x, y, width, height, 0, GLOBAL.processing.color(255, 255, 255), 255);
		    	GLOBAL.gu.drawBox(x, y, width, height, 1, GLOBAL.processing.color(0, 0, 0), 180);
			}
		}
			
		if(label != null)
		{
			//GLOBAL.processing.textAlign(GLOBAL.processing.LEFT);
			GLOBAL.processing.strokeWeight(2);
			GLOBAL.processing.fill(GLOBAL.colorButtonLabel);
			GLOBAL.processing.textAlign(PApplet.CENTER, PApplet.CENTER);
			GLOBAL.processing.textFont(GLOBAL.tFont,fontSize);
			if(icon!=null)
				GLOBAL.processing.text(label, cx + (width/2) + 10, cy + (height/2));
			else
			{
				if(height == 38) //ageButtons
				{
					GLOBAL.processing.textFont(GLOBAL.tFont32);
					GLOBAL.processing.text(label, cx + (width/2), cy + (height/2));
				}
				else
				{
					GLOBAL.processing.textFont(GLOBAL.tFont,fontSize);
					GLOBAL.processing.text(label, cx + (width/2), cy + (height/2));
				}
			}
				
		}

		if(icon != null)
		{
			GLOBAL.processing.imageMode(PApplet.CENTER);
			if(width == 18)
				GLOBAL.processing.image(icon, x+width/2, y+height/2, 14+(cx-x),14+(cy-y));
			else if(width == 190) //sexButton
				GLOBAL.processing.image(icon, x+width/2, y+height/2, 0.8f*(width-(cx-x)-5),0.8f*(width-(cy-y)-5));
			else
				GLOBAL.processing.image(icon, x+5, y+5, 16,16);
			
		}
		
		if(dragged == true)
			mouseDragged();
	}
	
	public void mouseReleased()
	{
		if (mouseOver() || dragged == true /*&& dragged into specific view area*/)
		{
			active = !active;
		}
		if(dragged == true && GLOBAL.onDynTree == true)
			GLOBAL.draggedToDynTree = true;
		if(dragged == true && GLOBAL.onCompareRegion == true) {
			boolean doNothing = false;
			for(Artist a:GLOBAL.artistsCompareList) {
				if(a.id == artist.id) {
					doNothing = true;
				}
			}
			if(!doNothing) {
				for(int i = 0; i<4; i++) {
					if(GLOBAL.block[i].mouseOver()) {
						if(i<GLOBAL.artistsCompareList.size()) {
							
							GLOBAL.artistsCompareList.set(i, artist);
							GLOBAL.block[i].sexGPie.updateNumber(GLOBAL.testGPie.number[0], GLOBAL.testGPie.number[1]);
							GLOBAL.block[i].ageBar.setData(GLOBAL.testBar.barElements);
							GLOBAL.block[i].countryPie.setData(GLOBAL.testPie.pieElements);
							GLOBAL.block[i].image = GLOBAL.testImage;
						} else {
							GLOBAL.artistsCompareList.add(artist);
							GLOBAL.block[GLOBAL.artistsCompareList.size()-1].sexGPie.updateNumber(GLOBAL.testGPie.number[0], GLOBAL.testGPie.number[1]);
							GLOBAL.block[GLOBAL.artistsCompareList.size()-1].ageBar.setData(GLOBAL.testBar.barElements);
							GLOBAL.block[GLOBAL.artistsCompareList.size()-1].countryPie.setData(GLOBAL.testPie.pieElements);
							GLOBAL.block[GLOBAL.artistsCompareList.size()-1].image = GLOBAL.testImage;
				}
					}
					GLOBAL.block[i].visible = false;
				}
				for(int i = 0; i< GLOBAL.artistsCompareList.size(); i++) {
//					GLOBAL.block[i].sexGPie.setPosition(460, 25+55+170*i);
//					GLOBAL.block[i].ageBar.setPosition(580, 55+170*i);
//					GLOBAL.block[i].countryPie.setPosition(740, 55+170*i);
					
					//TODO: Get the detail data from db!!!
					
					GLOBAL.block[i].visible = true;
				}
			}
//			GLOBAL.draggedToCompareRegion = true;
		}
			
		dragged = false;
		pressed = false;
		//if(GLOBAL.artistsCompare) {
			active = false;
		//}
	}
	
	public void mousePressed()
	{
		if(mouseOver())
		{
			pressed = true;
			dx = GLOBAL.processing.mouseX - x;
			dy = GLOBAL.processing.mouseY - y;
		}
	}
	
	public void mouseDragged()
	{
		if(prevPaintX!=0 || prevPaintY != 0)
		{
			//erase the previous shadow
			GLOBAL.processing.noStroke();
			GLOBAL.processing.fill(GLOBAL.colorBackground,0);
			GLOBAL.processing.rect(prevPaintX, prevPaintY, width, height);
		}
		GLOBAL.processing.stroke(GLOBAL.colorButtonActive);
		GLOBAL.processing.fill(GLOBAL.colorButtonActive,5);
		GLOBAL.processing.rect(GLOBAL.processing.mouseX - dx, GLOBAL.processing.mouseY - dy, width, height);
		prevPaintX = GLOBAL.processing.mouseX;
		prevPaintY = GLOBAL.processing.mouseY;
		
		if(label != null)
		{
//			PApplet.println(label);
			//GLOBAL.processing.textAlign(GLOBAL.processing.LEFT);
			//GLOBAL.processing.strokeWeight(1);
			GLOBAL.processing.fill(GLOBAL.colorButtonLabel);
			GLOBAL.processing.textAlign(PApplet.CENTER, PApplet.CENTER);
			GLOBAL.processing.textFont(GLOBAL.tFont,fontSize);
			if(icon!=null)
				GLOBAL.processing.text(label, GLOBAL.processing.mouseX - dx + (width/2) + 10, GLOBAL.processing.mouseY - dy + (height/2));
			else
				GLOBAL.processing.text(label, GLOBAL.processing.mouseX - dx + (width/2), GLOBAL.processing.mouseY - dy + (height/2));
		}

		if(icon != null)
		{
			GLOBAL.processing.image(icon, x + 5, x + 5, 18,18);
		}
	}
	
	
}

